package com.kehongborui.station.dao.impl;

import com.kehongborui.station.dao.IStationDao;
import com.kehongborui.station.entity.Ticket;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @Description: 类描述
 * @Author: XCQ
 * @email: mr_xcq@163.com
 * @CreateDate: 2018-11-19 20:23
 * @UpdateUser: username
 * @UpdateDate: xxxx-xx-xx
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class StationDao implements IStationDao {
    @Override
    public void create(Ticket ticket) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conne = DriverManager.getConnection("jdbc:mysql://localhost:3306/station","root","wwwm1905com");
            String sql = "INSERT INTO ticket (id,start_time,stop_time,start_station,stop_station,price,stock,`type`,create_time,update_time)" +
                    " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement statement = conne.prepareStatement(sql);
            statement.setInt(1,ticket.getId());
            statement.setDate(2,ticket.getStartTime());
            statement.setDate(3,ticket.getStopTime());
            statement.setString(4,ticket.getStartStation());
            statement.setString(5,ticket.getStopStation());
            statement.setInt(6,ticket.getPrice());
            statement.setInt(7,ticket.getStock());
            statement.setString(8,ticket.getType());
            statement.setDate(9,ticket.getCreateTime());
            statement.setDate(10,ticket.getUpdateTime());
            statement.executeUpdate();
            statement.close();
            conne.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Ticket ticket) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conne = DriverManager.getConnection("jdbc:mysql://localhost:3306/station","root","wwwm1905com");
            String sql = "UPDATE ticket SET start_time=?,start_station=?,stop_station=?,price=?,stock=? WHERE id=?";
            PreparedStatement statement = conne.prepareStatement(sql);
            statement.setDate(1,ticket.getStartTime());
            statement.setString(2,ticket.getStartStation());
            statement.setString(3,ticket.getStopStation());
            statement.setInt(4,ticket.getPrice());
            statement.setInt(5,ticket.getStock());
            statement.setInt(6,ticket.getId());
            statement.executeUpdate();
            statement.close();
            conne.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Ticket select(int id) {
        Ticket ticket = new Ticket();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conne = DriverManager.getConnection("jdbc:mysql://localhost:3306/station","root","wwwm1905com");
            String sql = "SELECT * FROM ticket WHERE id=?";
            PreparedStatement statement = conne.prepareStatement(sql);
            statement.setInt(1,id);
            ResultSet result = statement.executeQuery();
            while (result.next()) {
                ticket.setId(id);
                ticket.setStartTime(result.getDate("start_time"));
                ticket.setStopTime(result.getDate("stop_time"));
                ticket.setStartStation(result.getString("start_station"));
                ticket.setStopStation(result.getString("stop_station"));
                ticket.setPrice(result.getInt("price"));
                ticket.setStock(result.getInt("stock"));
                ticket.setType(result.getString("type"));
                ticket.setCreateTime(result.getDate("create_time"));
                ticket.setUpdateTime(result.getDate("update_time"));
            }
            result.close();
            statement.close();
            conne.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ticket;
    }

    @Override
    public void delete(int id) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conne = DriverManager.getConnection("jdbc:mysql://localhost:3306/station","root","wwwm1905com");
            String sql = "DELETE FROM ticket WHERE id=?";
            PreparedStatement statement = conne.prepareStatement(sql);
            statement.setInt(1,id);
            statement.executeUpdate();
            statement.close();
            conne.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
